//
//  JGRealTimeOrderController.m
//  JGRider
//
//  Created by 单启志 on 2018/1/25.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGRealTimeOrderController.h"
#import "JGRealTimeOrderCell.h"
#import "WJYAlertView.h"
#import "JGRealOrderAlertView.h"
#import "JGAuthenticationController.h"//身份验证
#import "JGRealOrderListApi.h"
#import "JGPlaceOrderApi.h"
#import "JGRealTimeOrderModel.h"
#import "JGHomeController.h"
@interface JGRealTimeOrderController ()<UITableViewDelegate,UITableViewDataSource,JGRealTimeOrderCellDelegate,ZJScrollPageViewChildVcDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)JGRealOrderAlertView *alertContentView;
@property(nonatomic,strong)WJYAlertView *alertView;
@property(nonatomic,strong)NSArray<JGRealTimeOrderModel*> *datas;
@property(nonatomic,strong)JGRealOrderListApi *api;

@end

@implementation JGRealTimeOrderController
static NSString *cellId=@"realTimeOrderCell";

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [self configSubViews];

    [self.tableView.mj_header beginRefreshing];

}

- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    NSLog(@"zj_viewWillAppearForIndex:%ld",index);
}
-(void)zj_viewDidAppearForIndex:(NSInteger)index{
    NSLog(@"zj_viewDidAppearForIndex:%ld",index);
    [self.tableView.mj_header beginRefreshing];
}

- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    NSLog(@"zj_viewWillDisappearForIndex:%ld",index);
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    NSLog(@"zj_viewDidDisappearForIndex:%ld",index);
}

#pragma mark - 私有方法
-(void)getMoreData{
    [self getDataLoadMore:YES];

}
-(void)getNewData{
    [self getDataLoadMore:NO];
}
//
-(void)getDataLoadMore:(BOOL)isMore{

    self.view.blankPageView.hidden=YES;
    FQWeakSelf(self);
    [self.api getRealOrderListMid:LoginModel.Id isMore:isMore successBlock:^(id result) {
        [weakself refreshViewIsError:NO];

    } faileBlock:^(id error) {
        NSLog(@"一直请求吗？？");

         [weakself refreshViewIsError:YES];

    }];

}

//滚动到已结单界面
-(void)scrollToHaveOrderView{
    JGHomeController*homeVc= (JGHomeController*)self.zj_scrollViewController;
    [homeVc.scrollPageView setSelectedIndex:1 animated:YES];
}

//根据api请求结果显示界面
-(void)refreshViewIsError:(BOOL)isError{
    FQWeakSelf(self);
    self.view.blankPageView.hidden=NO;
    [self.view configBlankPage:EaseBlankPageTypeView hasData:self.api.datas.count hasError:isError reloadButtonBlock:^(id sender) {
//        JGpostNotification(PlaceOrderSuccessNoti, nil);
//        [weakself scrollToHaveOrderView];
        [weakself.tableView.mj_header beginRefreshing];
    }];
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}

-(void)configSubViews{
    self.title=@"实时订单";
    [self.view addSubview:self.tableView];


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - cell delegate
//抢单
-(void)cellPlaceOrderEvent:(UITableViewCell *)cell{

    NSIndexPath *index=[self.tableView indexPathForCell:cell];

   JGRealTimeOrderModel*model= self.api.datas[index.row];

    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"type"]=@"1";
    params[@"buyerId"]=model.buyerCompId;
    params[@"sellerId"]=LoginModel.Id;
    params[@"orderId"]=@(model.Id);
    
    FQWeakSelf(self);
    [[JGRequestHelper share]requestWithParams:params requestApi:[JGPlaceOrderApi class] hudMessage:@"抢单中..." view:nil successBlock:^(id result) {

       NSString *state=result[@"data"][@"state"];
        if ([state isEqualToString:@"0"]) {
            weakself.alertContentView.type=ResultTypeSuccess;
            [weakself.alertView show];
        }
    } faileBlock:^(id error) {
        [MBProgressHUD showError:@"失败" ToView:nil];
    }];
//    self.alertContentView.type=arc4random_uniform(10)%3;
//    [self.alertView show];
}
//弹框点击确定按钮功能
-(void)alertViewConfirmBtnEvent{

    ResulType type=self.alertContentView.type;
    if (type==ResultTypeSuccess) {//成功
        //通知刷新
        JGpostNotification(PlaceOrderSuccessNoti, nil);
        [self scrollToHaveOrderView];
    }else if(type==ResultTypeWasRobbed){//被抢

    }else if (type==ResultTypeNoValidate){//未注册
        JGAuthenticationController *vc=[[JGAuthenticationController alloc]init];
        JGpushViewController(vc);
    }
}
#pragma mark - tableview delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //return 10;
    return self.api.datas.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        CGFloat height=[tableView fd_heightForCellWithIdentifier:cellId configuration:^(JGRealTimeOrderCell *cell) {
         cell.model=self.api.datas[indexPath.row];
        }];
    JGLog(@"%.2f",height);
    return height+25;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    JGRealTimeOrderCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    cell.model=self.api.datas[indexPath.row];
    cell.delegate=self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


}

#pragma mark - getter setter
-(UITableView *)tableView{

    if (!_tableView) {

        _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,0, JGScreenW, self.view.height-64-44-44) style:UITableViewStylePlain];

        _tableView.backgroundColor=HEXCOLOR(0xf2f2f2);
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JGRealTimeOrderCell class]) bundle:nil] forCellReuseIdentifier:cellId];

        _tableView.showsVerticalScrollIndicator=NO; _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.delegate=self;
        _tableView.dataSource=self;

        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];

        _tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    }

    return _tableView;
}

-(WJYAlertView *)alertView{
    if (_alertView==nil) {
        _alertView=[[WJYAlertView alloc]initWithCustomView:self.alertContentView dismissWhenTouchedBackground:NO];
    }
    return _alertView;
}

-(JGRealOrderAlertView *)alertContentView{
    if (_alertContentView==nil) {
        _alertContentView=[[JGRealOrderAlertView alloc]initWithFrame:CGRectMake(0, 0, AdaptedWidth(211), AdaptedHeight(121))];
        FQWeakSelf(self);
        _alertContentView.confirmBlock=^(){
            [weakself alertViewConfirmBtnEvent];
        };
    }

    return _alertContentView;
}

-(JGRealOrderListApi *)api{
    if (!_api) {
        _api=[[JGRealOrderListApi alloc]init];
    }
    return _api;
}
@end
